angular.module("dashboardApp")
.filter('catFilter', function() {
	return function(array, vn) {
		arrx = [];
		if(vn) {
			angular.forEach(array, function(cat) {
				id = cat._id;
				if (id.indexOf('vn') != -1) arrx.push(cat);
			});
			return arrx;
		}
		else {
			angular.forEach(array, function(cat) {
				id = cat._id;
				if (id.indexOf('tg') != -1) arrx.push(cat);
			});
			return arrx;
		}
	}
})

.filter('vnSort', function() {
	convert=function(str, index) {
		str = str.toLowerCase();
		str=str.replace("á","a1"); str=str.replace("à","a2"); str=str.replace("ả","a3"); str=str.replace("ã","a4"); str=str.replace("ạ","a5");
		str=str.replace("ă","az");str=str.replace("ắ","az1"); str=str.replace("ằ","az2"); str=str.replace("ẳ","az3"); str=str.replace("ẵ","az4"); str=str.replace("ặ","az5");
		str=str.replace("â","azz");str=str.replace("ấ","azz1"); str=str.replace("ầ","azz2"); str=str.replace("ẩ","azz3"); str=str.replace("ẫ","azz4"); str=str.replace("ậ","azz5");
		str=str.replace("ó","o1"); str=str.replace("ò","o2"); str=str.replace("ỏ","o3"); str=str.replace("õ","o4"); str=str.replace("ọ","o5");
		str=str.replace("ô","oz");str=str.replace("ố","oz1"); str=str.replace("ồ","oz2"); str=str.replace("ổ","oz3"); str=str.replace("ỗ","oz4"); str=str.replace("ộ","oz5");
		str=str.replace("ơ","ozz");str=str.replace("ớ","ozz1"); str=str.replace("ờ","ozz2"); str=str.replace("ở","ozz3"); str=str.replace("ỡ","ozz4"); str=str.replace("ợ","ozz5");
		str=str.replace("ú","u1"); str=str.replace("ù","u2"); str=str.replace("ủ","u3"); str=str.replace("ũ","u4"); str=str.replace("ụ","u5");
		str=str.replace("ư","uz");str=str.replace("ứ","uz1"); str=str.replace("ừ","uz2"); str=str.replace("ử","uz3"); str=str.replace("ữ","uz4"); str=str.replace("ự","uz5");
		str=str.replace("é","e1"); str=str.replace("è","e2"); str=str.replace("ẻ","e3"); str=str.replace("ẽ","e4"); str=str.replace("ẹ","e5");
		str=str.replace("ê","ez"); str=str.replace("ế","ez1"); str=str.replace("ề","ez2"); str=str.replace("ể","ez3"); str=str.replace("ễ","ez4"); str=str.replace("ệ","ez5");
		str=str.replace("í","i1"); str=str.replace("ì","i2"); str=str.replace("ỉ","i3"); str=str.replace("ĩ","i4"); str=str.replace("ị","i5");
		str=str.replace("ý","y1"); str=str.replace("ỳ","y2"); str=str.replace("ỷ","y3"); str=str.replace("ỹ","y4"); str=str.replace("ỵ","y5");
		str=str.replace("đ","dz");
		return { 'str': str, 'i': index }
		// return str;
	}
	return function(array, type) {
		arr = [];
		arrx = [];
		arrSort = [];

		angular.forEach(array, function(item) {
			arr.push(item);
		});

		angular.forEach(array, function(item, i) {
			cvt = convert(item[type], i);
			arrSort.push(cvt);
		});
		arrSort.sort(function(a, b) {
			if(a.str > b.str) return 1;
			if(a.str < b.str) return -1;
			return 0;
		});

		angular.forEach(arrSort, function(item) {
			arrx.push(arr[item.i]);
		})
		return arrx;
	}
})

.filter('simpleFilter', function() {

	removeSign = function(str) {
		str = str.toLowerCase();
		str = str.replace(/ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ/g,"o");
		str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");
		str = str.replace(/é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ/g,"e");
		str = str.replace(/ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự/g,"u");
		str = str.replace(/í|ì|ỉ|ĩ|ị/g,"i");
		str = str.replace(/ý|ỳ|ỷ|ỹ|ỵ/g,"y");
		str = str.replace(/đ/g,"d");
		return str;
	}

	return function(array, str, type) {
		arrx = [];
		str = removeSign(str);

		angular.forEach(array, function(item) {
			desctrip = removeSign(item[type]);
			if(desctrip.search(str) != -1)
				arrx.push(item);
		});
			
		return arrx;
	}
})

.filter('removeLesson', function() {
	return function(input) {
		str = input;
		if(str) {
			index = str.search(/bài /i);
			if(index == 0 && str[4] >= '0' && str[4] <= '9') {
				arr = str.split(': ');
				arr.splice(0,1);
				str = arr.join();
				return str; 
			}
			return str;
		}
	}
})